package com.lvtongka.app.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.*
import android.widget.TextView
import com.lvtongka.app.R
import com.lvtongka.app.common.DialogCountDownTimer
import com.lvtongka.app.common.listener.CountDownCallback
import com.lvtongka.app.databinding.DialogRegisterSuccessBinding

open class RegisterSuccessDialog(context: Context, theme: Int) : Dialog(context, theme), View.OnClickListener {
    private var binding: DialogRegisterSuccessBinding = DialogRegisterSuccessBinding.inflate(layoutInflater)
    private var downTimer: DialogCountDownTimer? = null
    init {
        initDialog()
    }

    private fun initDialog() {
        window!!.decorView.setPadding(0, 0, 0, 0)
        window!!.setGravity(Gravity.RELATIVE_LAYOUT_DIRECTION or Gravity.CENTER)
        val lp = window!!.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = lp
        setCanceledOnTouchOutside(false)
        show()
    }

    open fun hideDialog() {
        cancel()
        dismiss()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(binding.root)
        initView()
        initListener()
    }

    private fun initView() {
        downTimer = DialogCountDownTimer(
            binding.tvGoLogin,
            3000L,
            1000L,
            object : CountDownCallback {
                override fun onFinish() {
                    downTimer = null
                    mListener?.onItemClick()
                }
            })
        downTimer?.start()

    }

    private fun initListener() {
        binding.tvGoLogin.setOnClickListener(this)
        setOnKeyListener { _, keyCode, event ->
            false
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (isOutOfBounds(context, event!!)) {
            downTimer?.cancel()
            downTimer = null
            hideDialog()
            mListener?.onItemClick() //触摸dialog外部关闭dialog
        }
        return super.onTouchEvent(event)
    }

    private fun isOutOfBounds(context: Context, event: MotionEvent): Boolean {
        val x = event.x.toInt()
        val y = event.y.toInt()
        val slop = ViewConfiguration.get(context).scaledWindowTouchSlop
        val decorView = window!!.decorView
        return (x < -slop) || (y < -slop) || (x > (decorView.width + slop))
                || (y > (decorView.height + slop))
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tvGoLogin -> {
                hideDialog()
                mListener?.onItemClick() //点击“取消”关闭dialog
            }
        }
    }

    private var mListener: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onItemClick()
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.mListener = listener
    }

}